package com.wcgksoft.controller.equipment;

import com.wcgksoft.common.util.Layui;
import com.wcgksoft.entity.Equipment.DataType;
import com.wcgksoft.service.Equipment.DataTypeService;
import net.sf.json.JSONObject;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 周权
 * @Date: 2020/03/08/16:32
 * @Description:数据类型
 */
@Controller
@RequestMapping(value = "system/datatype")
public class DataTypeController {
    @Autowired
    private DataTypeService dataTypeService;

    /*
     * @Author: 周权
     * @Description:数据类型列表页面layui的table加载数据
     * @Param:
     * @create: 2020/3/6 15:00
     * @return:
     */
    @ResponseBody
    @RequestMapping(value = "findAll")
    public Layui findAll(@RequestParam(value = "e_id") Integer e_id,
                         @RequestParam(value = "page", required = false) Integer page,
                         @RequestParam(value = "limit", required = false) Integer limit) {
        DetachedCriteria criteria = DetachedCriteria.forClass(DataType.class);
        criteria.add(Restrictions.eq("e_id", e_id));
        List<DataType> byConditions = dataTypeService.findByConditions(criteria,page,limit);
        return Layui.data(0, "查询成功", dataTypeService.getTotalCount(), byConditions);
    }

    /*
     * @Author: 周权
     * @Description: 数据类型列表页面的添加/修改数据
     * @Param:
     * @create: 2020/3/6 15:07
     * @return:
     */
    @ResponseBody
    @RequestMapping(value = "update")
    public Layui update(@RequestParam(value = "data", required = false) String data){
        JSONObject param = JSONObject.fromObject(data);
        if (param == null) {
            return Layui.data(1, "请求格式有误", 0, null);
        } else {
            String id1 = String.valueOf(param.get("id"));
            if(null==id1||id1.equals("")){
                DataType dataType = (DataType) JSONObject.toBean(JSONObject.fromObject(data), DataType.class);
                dataType.setD_createDate(new Date());
                dataTypeService.save(dataType);
                return Layui.data(0, "添加成功", 1, Layui.addList(dataType));
            }else{
                DataType dataType = (DataType) JSONObject.toBean(JSONObject.fromObject(data), DataType.class);
                dataType.setD_createDate(new Date());
                dataTypeService.saveOrUpdate(dataType);
                return Layui.data(0, "修改成功", 1, Layui.addList(dataType));
            }
        }
    }

    /*
     * @Author: 周权
     * @Description:数据类型删除功能
     * @Param:
     * @create: 2020/3/8 10:20
     * @return:
     */
    @ResponseBody
    @RequestMapping(value = "delete")
    public Layui delete(@RequestParam(value = "id") Integer id) {
        DataType byId = dataTypeService.findById(id);
        dataTypeService.delete(byId);
        return Layui.data(1, "删除成功", 1, null);
    }
}
